# -*- coding: utf-8 -*-
import json,time
import web
urls = (
    '/ndflowlog', 'ndflowlog'
)
app = web.application(urls, globals())

class ndflowlog:        
    def GET(self):
        i = web.input()
        print i
        ndflowid = i.get('ndflowid','0')
        dataDay = i.get('dataDay','')
        callback = i.get('callback','')
        # if not (ndflowid and dataDay):
            # return render.error('id,day是必须的', None)
        db=web.database(dbn='sqlite',db='ndflow.db3')
        if dataDay=='ALL':
            results=db.select('ndflowlog', what='round(frx/1024) as frx,round(ftx/1024) as ftx,dtlog,icpe', where="ndflowid=$ndflowid",vars=dict(ndflowid=int(ndflowid)),_test=False)
        else:
            results=db.select('ndflowlog', what='round(frx/1024) as frx,round(ftx/1024) as ftx,dtlog,icpe', where="ndflowid=$ndflowid and date(dtlog)=$dataDay",vars=dict(ndflowid=int(ndflowid),dataDay=dataDay),_test=False)
        a=[]
        for r in results:
            a.append((int(r.frx),int(r.ftx),r.dtLog[:19],r.iCpe))       
        web.header('Content-Type', 'application/json')
        return callback+'('+json.dumps(a)+')'
        
        # 直接转成json
        # return json.dumps(list(results))
'''        
        pyDict = {'one':1,'two':2}
        web.header('Content-Type', 'application/json')
        return json.dumps(pyDict)
'''


if __name__ == "__main__":
    app.run()
    
    
